package com.yingjie.customcontrol;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

public class CommonMethod {
	public static JSONObject HashMap2JSON(HashMap<String,String> map){
		JSONObject jo = new JSONObject();
		Iterator iterator = map.keySet().iterator();
		try {
			while(iterator.hasNext()) {
				String key = (String)iterator.next();
				String value = map.get(key);
				jo.put(key, value);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return jo;
	}
	
    public static boolean isNum(String str){
    	if(isInt(str)){
    		return true; 
    	}
    	Pattern pattern = Pattern.compile("^(0|[1-9]\\d*)\\.(\\d+)$"); 
    	Matcher isNum = pattern.matcher(str); 
    	if( !isNum.matches() ) 
    	{ 
    	return false; 
    	} 
    	return true; 
    } 
    
    public static boolean isInt(String str){
    	Pattern pattern = Pattern.compile("^[0-9]*$"); 
    	Matcher isNum = pattern.matcher(str); 
    	if( !isNum.matches() ) 
    	{ 
    	return false; 
    	} 
    	return true; 
    }
    
    
	
    public static String getDate(){   
        String temp_str="";   
        Date dt = new Date();   
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");   
        temp_str=sdf.format(dt);   
        return temp_str;
    } 
    
	public static void Dial(Context context,String strTel){
		Uri uri = Uri.parse("tel:" +strTel);
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_CALL);
		intent.setData(uri);
		context.startActivity(intent);
	}
}
